package com.qinggeng.project.api.controller;

import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.DistributionPointDto;
import com.qinggeng.project.api.dto.ShareDto;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.DistributionPoint;
import com.qinggeng.project.system.service.IDistributionPointService;
import com.qinggeng.project.system.service.IShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.testng.collections.Maps;

import java.util.Map;

@Api(tags = "分享模块")
@RestController
@Slf4j
@RequestMapping("${apiPath}/share")
public class ApiShareController extends BaseController {

    @Autowired
    private IShareService shareService;
    @Autowired
    private IDistributionPointService distributionPointService;

    @ApiOperation("获取指定分享内容")
    @GetMapping("/get")
    @ApiImplicitParams(
            @ApiImplicitParam(name="id",value="分享内容ID",dataType="Long", paramType = "query",required = false)
    )
    public AjaxResult selectShareById(
            @RequestParam(value = "id",defaultValue = "1")Long shareId
    ){
        ShareDto share=shareService.selectShareDtoById(shareId);
        return AjaxResult.success(share);
    }

    @GetMapping("/getOldAndNewPoint")
    @LoginRequired(role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult getOldAndNewPoint(Long courierId,Long shopId,Long pointId,Long addressId,Float lng, Float lat){
        DistributionPointDto olddistributionPoint = distributionPointService.systemSelectPoint(shopId, pointId, addressId, lng, lat);
        if (olddistributionPoint == null){
            log.error("olddistributionPoint is null");
            throw new CustomException("olddistributionPoint is null");
        }
        DistributionPoint distributionPoint = distributionPointService.selectPointByCourierId(courierId);
        if (distributionPoint == null){
            log.error("distributionPoint is null");
            throw new CustomException("distributionPoint is null");
        }
        DistributionPointDto newdistributionPoint = new DistributionPointDto();
        BeanUtils.copyProperties(distributionPoint,newdistributionPoint);
        Map<String, DistributionPointDto> data = Maps.newHashMap();
        data.put("oldPoint",olddistributionPoint);
        data.put("newPoint",newdistributionPoint);
        return AjaxResult.success(data);

    }

}
